package fei.zhou.springboot2.exception;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: MyExceptionHandler
 * @date 2021/1/13  19:28
 * @Verson 1.0 -2021/1/13  19:28
 * @see
 */
@ControllerAdvice
public class MyExceptionHandler2 {
    @ExceptionHandler(Exception.class)
    public String handleException(Exception e, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();

        //设置错误状态码
        request.setAttribute("javax.servlet.error.status_code",500);
        map.put("code","500");
        map.put("message","500错误");

        request.setAttribute("ext",map);
        //转发到/error,那里会到浏览器和客户端情况分别进行设置
        return "forward:/error";
    }

}


